package kofman.test.cps;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class KofmanGPSActivity extends Activity  implements LocationListener  {
    /** Called when the activity is first created. */
	private boolean getService = false;
	private int m_nTime = 0;
	private Button click1;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		click1=(Button)findViewById(R.id.button);
		click1.setClickable(false);
		click1.setOnClickListener(clickListener);
		//取得系統定位服務
		LocationManager status = (LocationManager) (this.getSystemService(Context.LOCATION_SERVICE));
		if (status.isProviderEnabled(LocationManager.GPS_PROVIDER) || status.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
			//如果GPS或網路定位開啟，呼"叫locationServiceInitial()更新位置
			Log.d("CHANGE","locationServiceInitial");
			getService=true;
			locationServiceInitial();
		} else {
			Log.d("CHANGE","NO locationServiceInitial");
			Toast.makeText(this, "請開啟定位服務", Toast.LENGTH_LONG).show();
			startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));	//開啟設定頁面
		}
	}
 
	@Override
	public void onLocationChanged(Location arg0) {	//當地點改變時
		getLocation(location);
 
	}
 
	@Override
	public void onProviderDisabled(String arg0) {	//當GPS或網路定位功能關閉時
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void onProviderEnabled(String arg0) {	//當GPS或網路定位功能開啟
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {	//定位狀態改變
		// TODO Auto-generated method stub
 
	}
	
	public OnClickListener clickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		 Intent intent=new Intent(v.getContext(),mainActivity.class);
		 intent.putExtra("longitude", location.getLongitude());
		 intent.putExtra("latitude", location.getLatitude());
		 startActivityForResult(intent, 0);
		 
			
		}
	};
	private LocationManager lms;
	private Location location;
	private String bestProvider = LocationManager.GPS_PROVIDER;	//最佳資訊提供者
	private void locationServiceInitial() {
		lms = (LocationManager) getSystemService(LOCATION_SERVICE);	//取得系統定位服務
		Criteria criteria = new Criteria();	//資訊提供者選取標準
		bestProvider = lms.getBestProvider(criteria, true);	//選擇精準度最高的提供者
		location = lms.getLastKnownLocation(bestProvider);
		getLocation(location);
	}
	private void getLocation(Location location) {	//將定位資訊顯示在畫面中
		if(location != null) {
			TextView longitude_txt = (TextView) findViewById(R.id.longitude);
			TextView latitude_txt = (TextView) findViewById(R.id.latitude);
			TextView time_txt = (TextView) findViewById(R.id.time);
			Double longitude = location.getLongitude();	//取得經度
			long time=location.getTime();
			Double latitude = location.getLatitude();	//取得緯度
			SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			longitude_txt.setText(String.valueOf(longitude));
			latitude_txt.setText(String.valueOf(latitude));
			time_txt.setText(dateformat1.format(time));
			click1.setClickable(true);
		}
		else {
			Toast.makeText(this, "無法定位座標", Toast.LENGTH_LONG).show();
		}
	}
	protected void onResume() {
		super.onResume();
		handler.postDelayed(runnable,1000); // 开始Timer
		if(getService) {
			Log.d("CHANGE", "CHANGE");
			lms.requestLocationUpdates(bestProvider, 0, 0, this);
			//服務提供者、更新頻率60000毫秒=1分鐘、最短距離、地點改變時呼叫物件
		}
		
	}
	
	private Handler handler = new Handler( );

	private Runnable runnable = new Runnable( ) {
		public void run ( ) {
			locationServiceInitial();
			getLocation(location);
			Log.d("CHANGE", "CHANGE by handler");
			handler.postDelayed(this,10000);
		}
	};
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(getService) {
			handler.removeCallbacks(runnable);
			lms.removeUpdates(this);	//離開頁面時停止更新
		}
	}
	
}